﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using 纺织贸易管理系统;

namespace BLL
{
    public class 来货退卷单BLL
    {
        /// <summary>
        /// 删除单据
        /// </summary>
        /// <param name="danhao">要删除的单号</param>
        public static void Delete(string danhao)
        {
            var danju = Connect.dbcontext.Queryable<DanjuTable>().First(x => x.dh == danhao);
            var mingxis = Connect.dbcontext.Queryable<danjumingxitable>().Where(x => x.danhao == danhao).ToList();
            单据反审核(danju.dh);
            Connect.dbcontext.Deleteable(danju).ExecuteCommand();
            Connect.dbcontext.Deleteable(mingxis).ExecuteCommand();
        }
        /// <summary>
        /// 保存单据
        /// </summary>
        /// <param name="danju"></param>
        /// <param name="danjumingxitables"></param>
        public static void 保存(DanjuTable danju, List<danjumingxitable> danjumingxitables,List<JuanHaoTable> juanHaos)
        {
            if (SysInfo.GetInfo.own != "审核制")
            {
                danju.zhuangtai = "已审核";
            }
            else
            {
                danju.zhuangtai = "未审核";
            }
            danju.dh = 单据BLL.检查单号(danju.dh);
            DanjuTableService.InsertDanjuTable(danju);
            foreach (var m in danjumingxitables.Where(x => !string.IsNullOrWhiteSpace( x.pingming)).ToList())
            {
                m.danhao = danju.dh;
                m.CustomName = danju.ksmc;
                danjumingxitableService.Insertdanjumingxitable(m);
            }
            可发卷BLL.卷出库(juanHaos, danju.dh);
            if (SysInfo.GetInfo.own != string.Empty)
            {
                if (SysInfo.GetInfo.own != "审核制")
                {
                    单据审核(danju.dh);
                }
            }
            else
            {
                单据审核(danju.dh);
            }
        }
        /// <summary>
        /// 审核单据
        /// </summary>
        /// <param name="danhao"></param>
        public static void 单据审核(string danhao)
        {
            var danju = Connect.dbcontext.Queryable<DanjuTable>().First(x => x.dh == danhao);
            var danjumingxitables = Connect.dbcontext.Queryable<danjumingxitable>().Where(x => x.danhao == danhao).ToList();
            danju.zhuangtai = "已审核";
            Connect.dbcontext.Updateable(danju).ExecuteCommand();
            库存BLL.减少库存(danjumingxitables, danju);
            //销售发货单BLL.增加发货数量(danjumingxitables);
            增加退仓数量(danjumingxitables);
        }

        public static void 减少退仓数量(List<danjumingxitable> mingxis)
        {
            foreach (var m in mingxis)
            {
                var ruku = Connect.dbcontext.Queryable<danjumingxitable>().First(x => x.danhao == m.rkdh && x.pingming == m.pingming && x.yanse == m.yanse && x.ganghao == m.ganghao);
                if (ruku != null)
                {
                    ruku.ExitsNum -= m.chengpingmishu;
                    ruku.ExitsRoll -= m.chengpingjuanshu;
                    var inventory = 库存BLL.获取库存数(m);
                    ruku.InventoryQuantity =inventory.Quantity;
                    ruku.InventoryRolls = inventory.Rolls;
                    ruku.suolv = ruku.chengpingmishu - ruku.toupimishu-ruku.ExitsNum -ruku.InventoryQuantity;
                    Connect.dbcontext.Updateable<danjumingxitable>(ruku).ExecuteCommand();
                }
            }
        }
        public static void 增加退仓数量(List<danjumingxitable> mingxis)
        {
            foreach (var m in mingxis)
            {
                var ruku = Connect.dbcontext.Queryable<danjumingxitable>().First(x => x.danhao == m.rkdh && x.pingming == m.pingming && x.yanse == m.yanse && x.ganghao == m.ganghao);
                if (ruku != null)
                {
                    ruku.ExitsNum += m.chengpingmishu;
                    ruku.ExitsRoll += m.chengpingjuanshu;
                    var inventory = 库存BLL.获取库存数(m);
                    ruku.InventoryQuantity = inventory.Quantity;
                    ruku.InventoryRolls = inventory.Rolls;
                    ruku.suolv = ruku.chengpingmishu - ruku.toupimishu - ruku.ExitsNum - ruku.InventoryQuantity;
                    Connect.dbcontext.Updateable<danjumingxitable>(ruku).ExecuteCommand();
                }
            }
        }
        /// <summary>
        /// 单据反审核
        /// </summary>
        /// <param name="danhao"></param>
        public static void 单据反审核(string danhao)
        {
            var danju = Connect.dbcontext.Queryable<DanjuTable>().First(x => x.dh == danhao);
            var danjumingxitables = Connect.dbcontext.Queryable<danjumingxitable>().Where(x => x.danhao == danhao).ToList();
            danju.zhuangtai = "未审核";
            Connect.dbcontext.Updateable(danju).ExecuteCommand();
            库存BLL.增加库存(danjumingxitables, danju);
            //销售发货单BLL.减少发货数量(danjumingxitables);
            减少退仓数量(danjumingxitables);
            可发卷BLL.卷入库(danhao);
        }
        /// <summary>
        /// 修改单据
        /// </summary>
        /// <param name="danju"></param>
        /// <param name="danjumingxitables"></param>
        public static void 修改单据(DanjuTable danju, List<danjumingxitable> danjumingxitables,List<JuanHaoTable> juanHaos)
        {
            if (SysInfo.GetInfo.own != string.Empty)
            {
                if (SysInfo.GetInfo.own != "审核制")
                {
                    单据反审核(danju.dh);
                }
            }
            else
            {
                单据反审核(danju.dh);
            }
            Connect.dbcontext.Updateable(danju).ExecuteCommand();
            Connect.dbcontext.Deleteable<danjumingxitable>().Where(x => x.danhao == danju.dh).ExecuteCommand();
            danjumingxitableService.Deletedanjumingxitable(x => x.danhao == danju.dh);
            foreach (var m in danjumingxitables.Where(x =>!string.IsNullOrWhiteSpace( x.pingming)).ToList())
            {
                m.danhao = danju.dh;
                m.CustomName = danju.ksmc;
                danjumingxitableService.Insertdanjumingxitable(m);
            }
            可发卷BLL.卷出库(juanHaos, danju.dh);
            if (SysInfo.GetInfo.own != string.Empty)
            {
                if (SysInfo.GetInfo.own != "审核制")
                {
                    单据审核(danju.dh);
                }
            }
        }

    }
}
